package com.ysxm.service.userservice.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.ysxm.entity.Comment;
import com.ysxm.entity.Movies;
import com.ysxm.entity.Moviessorrt;
import com.ysxm.dao.usermapper.UserMapper;
import com.ysxm.entity.User;
import com.ysxm.service.userservice.UserService;
import com.ysxm.util.CustomException;
import com.ysxm.util.MailUtils;
import com.ysxm.util.MulitPartUtil;

@Service
@Transactional
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;

	// 用户注册
	public void userRegister(User user) {

		try {
			userMapper.userRegister(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 用户登录
	public User userLogin(String username, String password) {
		try {
			User user = userMapper.userLogin(username, password);
			return user;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 注册时判断用户名是否重复
	public boolean isName(String username) {
		boolean b;
		try {
			if (userMapper.isName(username) == null) {
				b = true;
			} else {
				b = false;
			}
			return b;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	// 注册时判断用户电话是否重复
	public boolean isPhone(String phone) {
		boolean b;
		try {
			if (userMapper.isPhone(phone) == null) {
				b = true;
			} else {
				b = false;
			}
			return b;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	// 注册时判断用户邮箱是否重复
	public boolean isEmail(String email) {
		boolean b;
		try {
			if (userMapper.isEmail(email) == null) {
				b = true;
			} else {
				b = false;
			}
			return b;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	// 用户查看个人信息
	public User lookupUser(Integer uid) {
		try {
			User user = userMapper.lookupUser(uid);
			return user;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 用户修改个人信息
	public void updateUser(User user) {
		try {
			userMapper.updateUser(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 用户查看影视列表
	public List<Movies> findAllMovies() {
		try {
			List<Movies> movies = userMapper.findAllMovies();
			return movies;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 用户查看影视
	public Movies findMoviesById(Integer mid) {
		try {
			Movies movies = userMapper.findMoviesById(mid);
			return movies;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 用户查看影视分类
	public List<Moviessorrt> findAllMoviesSort() {
		try {
			List<Moviessorrt> moviessorrts = userMapper.findAllMoviesSort();
			return moviessorrts;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 用户查看所属分类下的影视
	public Moviessorrt findMoviesSortById(Integer msid) {
		try {
			Moviessorrt moviessorrt = userMapper.findMoviesSortById(msid);
			return moviessorrt;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	// 用户通过搜索框查询影视
	public List<Movies> searchMovies(String moviesname) {
		try {
			List<Movies> movies = userMapper.searchMovies(moviesname);
			return movies;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	// 用户添加视频到我的收藏
	public void addCollect(Integer uid, Integer mid) {
		try {
			userMapper.addCollect(uid, mid);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException("添加收藏失败");
		}
	}

	// 用户查看我的收藏
	public List<Movies> lookAllCollect(Integer uid) {
		List<Movies> movies = null;
		try {
			movies = userMapper.lookAllCollect(uid);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException("查看收藏失败");
		}
		if (movies.size() == 0) {
			throw new CustomException("你还没有任何收藏");
		} else {
			return movies;
		}
	}

	// 用户删除收藏
	public void deleteCollect(Integer[] cid) {
		try {
			userMapper.deleteCollect(cid);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException("删除收藏失败");
		}

	}

	// 添加观看历史
	public void addRecord(Integer uid, Integer mid) {
		try {
			userMapper.addRecord(uid, mid);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException("添加观看历史失败");
		}

	}

	// 用户查看观看历史
	public List<Movies> lookAllRecord(Integer uid) {
		List<Movies> movies = null;
		try {
			movies = userMapper.lookAllRecord(uid);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException("查看观看历史失败");
		}
		if (movies.size() == 0) {
			throw new CustomException("你还没有观看影视");
		} else {
			return movies;
		}

	}

	// 用户删除观看历史
	public void deleteRecord(Integer[] rid) {
		try {
			userMapper.deleteRecord(rid);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException("删除观看历史失败");
		}
	}

	// 用户发表评论
	public Comment addComment(Comment comment) {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String mtime = sdf.format(date.getTime());
		comment.setCommenttime(mtime);
		try {
			userMapper.addComment(comment);
			System.out.println(comment.getCid());
			return userMapper.lookCommentById(comment.getUid(), comment.getCid());

		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException("添加评论失败");
		}
	}

	// 用户查看评论
	public List<Comment> lookComment(Integer uid) {
		List<Comment> commentnames = null;
		try {
			commentnames = userMapper.lookComment(uid);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException("查看评论失败");
		}
		if (commentnames.size() == 0) {
			throw new CustomException("你还没有发表任何评论");
		} else {
			return commentnames;
		}
	}

	// 根据id查看评论
	public Comment lookCommentById(Integer uid, Integer cid) {
		try {
			Comment comment = userMapper.lookCommentById(uid, cid);
			return comment;
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException("查看评论失败");
		}
	}

	// 用户删除评论
	public void deleteComment(Integer[] cid) {
		try {
			userMapper.deleteComment(cid);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException("删除评论失败");
		}
	}

	// 用户在评论界面删除评论
	public void deleteCommentById(Integer uid, Integer cid) {
		try {
			userMapper.deleteCommentById(uid, cid);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException("删除评论失败");
		}

	}

	// 用户查看一个影视下的所有评论
	public List<Comment> findAllComment(Integer mid) {
		List<Comment> comments = null;
		try {
			comments = userMapper.findAllComment(mid);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException("查看评论失败");
		}
		if (comments.size() == 0) {
			throw new CustomException("该影视没有评论，快来添加评论吧！");
		} else {
			return comments;
		}
	}

	// 用户找回密码
	public void updatePassword(String email,String password) {

		try {
			userMapper.updatePassword(email,password);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException("找回密码失败");
		}
	}
}
